//#include "main.h"
#include "bsp_Stm32fx.h"
#include "stm32f10x.h"
/* TencentOS头文件 */
#include "cmsis_os.h"

#include "lcd.h"
#include "key.h"

//task_start
#define TASK_START_SIZE    256 //任务栈大小
void task_start(void *pdata);//任务函数
osThreadDef(task_start, osPriorityNormal, 1, TASK_START_SIZE);//创建TASK_start任务

//task1
#define TASK1_STK_SIZE		256//任务栈大小
void task1(void *pdata);//任务函数
osThreadDef(task1, osPriorityNormal, 1, TASK1_STK_SIZE);//创建TASK1任务

//task2
#define TASK2_STK_SIZE		256//任务栈大小
void task2(void *pdata);//任务函数
osThreadDef(task2, osPriorityNormal, 1, TASK2_STK_SIZE);//创建TASK2任务


void BoardInit(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//移植操作系统需要设置为中断分组4 
	BSP_SysTickInit();
	BSP_GpioInit();
	BSP_UartInit(115200);
}


int main(void)
{ 
  	vu8 key=0;	
	BoardInit(); 	
    LCD_Init();			   	//初始化LCD 
    BSP_SetLed0(1); 	
    BSP_SetLed1(0);
	printf("Welcome to TencentOS tiny,CoreClock:%d\r\n",SystemCoreClock);  

    LCD_DisplayOn();
    POINT_COLOR=RED;		//设置字体为红色 
    POINT_COLOR=RED;		//设置字体为红色 
	LCD_ShowString(30,50,200,16,16,"TobudOs!");	
	LCD_ShowString(30,110,200,16,16,"2024/7/6");		
//    osKernelStart();//Start TOS Tiny

 while (1) {
        key = KEY_Scan(1); // 获取按键状态
        printf("Key State: %d\n", key); // 打印按键状态，调试用
        if (key == KEY0_PRES) {
            LCD_ShowString(30, 180, 200, 16, 16, "Key Pressed");
            break;
        } else {
            LCD_ShowString(30, 200, 200, 16, 16, "Press KEY0_PRES to Start Task Scheduler!!!");
            delay_xms(500); // 延时500ms
        }
    }
    osKernelInitialize(); 
    osThreadCreate(osThread(task_start), NULL); // 创建任务
    osKernelStart(); // 启动调度器
    return 0;   
}




void task_start(void *pdata)    //task_start 用于创建任务1和任务2
    {

        printf("task_start is running! Creating tasks 1 and 2...\r\n");
        osThreadCreate(osThread(task1), NULL); // Create task1
        osThreadCreate(osThread(task2), NULL); // Create task2
        printf("Tasks 1 and 2 created. Task0 is ending...\r\n");
        osThreadTerminate(osThreadGetId()); // End task0
    }

    
void task1(void *pdata)
{
    int count = 1;
    char buf[20];
    
    while(1)
    {
        // 打印到终端
        if(count % 2 == 1)
        {
            printf("\r\nHello world!\r\n### Task1 is running! , count is %d \r\n", count);
            sprintf(buf, "Task1 count: %d", count);
            // 清除原来的显示
            LCD_Fill(30, 140, 230, 160, WHITE);
            // 显示文本
            LCD_ShowString(30, 140, 200, 16, 16, (u8*)buf);
        } 
        count++;
        LED0_TOGGLE();
        osDelay(1000);  
    }
}



void task2(void *pdata)
{
    int count = 1;
    char buf[20];
    
    while(1)
    {
        // 打印到终端
        if(count % 2 == 0)
        {
            printf("\r\nHello TencentOS !\r\n*** Task2 is running! , count is %d \r\n", count);
            sprintf(buf, "Task2 count: %d", count);    
            // 清除原来的显示
            LCD_Fill(30, 160, 230, 180, WHITE);
            // 显示文本
            LCD_ShowString(30, 160, 200, 16, 16, (u8*)buf);
        }
        count++;
        LED1_TOGGLE();
        osDelay(1000);
    }
}
